package com.zy.je;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MyBatisPlusGenerator {
    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    //注意：
    //1、用户名、密码不能为null
    //2、必须引入velocity-engine-core依赖包

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:sqlite:/D:/doc/项目/jebook/metadata.db", "", "")
                // 全局配置
                .globalConfig((scanner, builder) -> {
                    builder.author(scanner.apply("请输入作者名称？")).fileOverride();
                    builder.enableSwagger();
                    builder.outputDir("D:/tmp/jegen");
                })
                // 包配置
                .packageConfig((scanner, builder) -> {
                    builder.parent(scanner.apply("请输入包名？"));
                })
                // 策略配置
                .strategyConfig((scanner, builder) -> {
                    builder.addInclude(MyBatisPlusGenerator.getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                            .addExclude(
                                    "meta",
                                    "tag_browser_authors",
                                    "tag_browser_filtered_authors",
                                    "tag_browser_filtered_publishers",
                                    "tag_browser_filtered_ratings",
                                    "tag_browser_filtered_series",
                                    "tag_browser_filtered_tags",
                                    "tag_browser_publishers",
                                    "tag_browser_ratings",
                                    "tag_browser_series",
                                    "tag_browser_tags",
                                    "tag_browser_custom_column_1",
                                    "tag_browser_filtered_custom_column_1"
                            )
                            .controllerBuilder().enableRestStyle().enableHyphenStyle().build();
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImp")
                            .build();
                    //entity的策略配置
                    builder.entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .enableActiveRecord()
//                            .versionColumnName("version")
//                            .logicDeleteColumnName("is_delete")
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .idType(IdType.ASSIGN_ID)
                            .formatFileName("%sEntity")
                            .build();
                })
                .execute();
    }
}

